﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsWorkCategoriesTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// WorkCategories Business Object
    /// </summary>

    public class WorkCategories : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected WorkCategories() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// UserID.
        /// </summary>
        public int UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    if (OnPropertyChanging("UserID", _userID, value))
                    {
                        int oldValue = _userID;
                        _userID = value;
                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private int _userID = -1;


        /// <summary>
        /// JobCategoryID.
        /// </summary>
        public int JobCategoryID
        {
            get { return _jobcategoryID; }
            set
            {
                if (_jobcategoryID != value)
                {
                    if (OnPropertyChanging("JobCategoryID", _jobcategoryID, value))
                    {
                        int oldValue = _jobcategoryID;
                        _jobcategoryID = value;
                        OnPropertyChanged("JobCategoryID", oldValue, value);
                    }
                }
            }
        }  private int _jobcategoryID = -1;


        /// <summary>
        /// JobSubCategoriesID.
        /// </summary>
        public int JobSubCategoriesID
        {
            get { return _jobSubCategoriesID; }
            set
            {
                if (_jobSubCategoriesID != value)
                {
                    if (OnPropertyChanging("JobSubCategoriesID", _jobSubCategoriesID, value))
                    {
                        int oldValue = _jobSubCategoriesID;
                        _jobSubCategoriesID = value;
                        OnPropertyChanged("JobSubCategoriesID", oldValue, value);
                    }
                }
            }
        }  private int _jobSubCategoriesID = -1;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty WorkCategories object.
        /// </summary>
        public static WorkCategories CreateNew()
        {
            WorkCategories obj = new WorkCategories();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static WorkCategories CreateNew(WorkCategoriesCollection collection)
        {
            WorkCategories obj = CreateNew();
            collection.Add(obj);
            return (obj);
        }

        #endregion

        #region GetWorkCategoriesByUserID

        /// <summary>
        /// Returns a collection of WorkCategories.
        /// </summary>
        public static WorkCategoriesCollection GetWorkCategoriesByUserID(int userid)
        {
            WorkCategoriesTableAdapter adapter = new WorkCategoriesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsWorkCategories.WorkCategoriesDataTable tbl = adapter.GetData(userid);

            WorkCategoriesCollection list = new WorkCategoriesCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsWorkCategories.WorkCategoriesRow row in tbl.Rows)
                {
                    WorkCategories WorkCategories = new WorkCategories();
                    RelationalToObject(WorkCategories, row);
                    list.Add(WorkCategories);
                }
            }

            return (list);
        }

        #endregion

        #region GetDistinctWorkCategoriesByUserID

        /// <summary>
        /// Returns a collection of WorkCategories.
        /// </summary>
        public static WorkCategoriesCollection GetDistinctWorkCategoriesByUserID(int userid)
        {
            WorkCategoriesTableAdapter adapter = new WorkCategoriesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsWorkCategories.WorkCategoriesDataTable tbl = adapter.GetDataForDistinctJobCategories(userid);

            WorkCategoriesCollection list = new WorkCategoriesCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsWorkCategories.WorkCategoriesRow row in tbl.Rows)
                {
                    WorkCategories WorkCategories = new WorkCategories();
                    RelationalToObject(WorkCategories, row);
                    list.Add(WorkCategories);
                }
            }

            return (list);
        }

        #endregion

        #region GetWorkCategoriesByUserIDnJobCategoryID

        /// <summary>
        /// Returns a collection of WorkCategories.
        /// </summary>
        public static WorkCategoriesCollection GetWorkCategoriesByUserIDnJobCategoryID(int userid, int jobcategoryid)
        {
            WorkCategoriesTableAdapter adapter = new WorkCategoriesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsWorkCategories.WorkCategoriesDataTable tbl = adapter.GetDataByJobCategoryID(userid,jobcategoryid);

            WorkCategoriesCollection list = new WorkCategoriesCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsWorkCategories.WorkCategoriesRow row in tbl.Rows)
                {
                    WorkCategories WorkCategories = new WorkCategories();
                    RelationalToObject(WorkCategories, row);
                    list.Add(WorkCategories);
                }
            }

            return (list);
        }

        #endregion

        #region GetWorkCategoriesByUserIDnJobSubCategoryID

        /// <summary>
        /// Returns a collection of WorkCategories.
        /// </summary>
        public static WorkCategoriesCollection GetWorkCategoriesByUserIDnJobSubCategoryID(int userid, int jobSubcategoryid)
        {
            WorkCategoriesTableAdapter adapter = new WorkCategoriesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsWorkCategories.WorkCategoriesDataTable tbl = adapter.GetDataByJobSubCategoryID(userid, jobSubcategoryid);

            WorkCategoriesCollection list = new WorkCategoriesCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsWorkCategories.WorkCategoriesRow row in tbl.Rows)
                {
                    WorkCategories WorkCategories = new WorkCategories();
                    RelationalToObject(WorkCategories, row);
                    list.Add(WorkCategories);
                }
            }

            return (list);
        }

        #endregion

        #region DeleteWorkCategoriesByUserID

        /// <summary>
        /// Updates the specified WorkCategories.
        /// </summary>
        public static void Delete(int userid)
        {
            WorkCategoriesTableAdapter adapter = new WorkCategoriesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            adapter.Delete(userid);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified WorkCategories.
        /// </summary>
        public static void Update(WorkCategories obj)
        {
            bool updateWorkCategories = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateWorkCategories = (obj.ObjState != ObjectState.Unchanged);

                if (!updateWorkCategories)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsWorkCategories.WorkCategoriesDataTable tbl = null;
            if (updateWorkCategories)
            {
                WorkCategoriesTableAdapter adapter = new WorkCategoriesTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsWorkCategories.WorkCategoriesDataTable();
                dsWorkCategories.WorkCategoriesRow row = tbl.NewWorkCategoriesRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("UserID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("WorkCategories: UserID={0}, JobCategoryID={1}, {2}", UserID, JobCategoryID));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more WorkCategories objects.
    /// </summary>

    public class WorkCategoriesCollection : List<WorkCategories>
    {
        #region Types

        public class UserComparer : IComparer<WorkCategories>
        {
            public int Compare(WorkCategories w1, WorkCategories w2)
            {
                return (String.Compare(w1.JobCategoryID.ToString(), w2.JobCategoryID.ToString()));
            }
        }

        #endregion

        #region Public Methods

        #region FindByJobCategoryID

        public WorkCategories FindByJobCategoryID(int jobcategoryID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].JobCategoryID == jobcategoryID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion

    }
}
